//定义电影演员相关接口
const express = require("express");
const router = express.Router();
//引入mysql连接池
const pool = require("../utils/db");
//引入joi表单验证模块
const Joi = require("joi");
//引入响应对象模块
const Response = require("../utils/Response");
//修改电影院接口
//接口地址/cinema/update
router.post("/cinema/update", (req, resp) => {
  let {
    id,
    cinema_name,
    address,
    province,
    city,
    district,
    longitude,
    latitude,
    tags,
  } = req.body;
  //表单验证
  let schema = Joi.object({
    id: Joi.string().required(),
    cinema_name: Joi.string().required(),
    address: Joi.string().required(),
    province: Joi.string().required(),
    city: Joi.string().required(),
    district: Joi.string().required(),
    longitude: Joi.number().required(),
    latitude: Joi.number().required(),
    tags: Joi.string().required(),
  });
  let { error, value } = schema.validate(req.body);
  if (error) {
    resp.send(Response.error(400, error));
    return;
  }
  //表单验证通过，执行添加操作
  let sql = `update movie_cinema set 
    cinema_name=?,
    address=?,
    province=?,
    city=?,
    district=?,
    longitude=?,
    latitude=?,
    tags=?
    where id = ?`;
  pool.query(
    sql,
    [
      cinema_name,
      address,
      province,
      city,
      district,
      longitude,
      latitude,
      tags,
      id,
    ],
    (error, result) => {
      if (error) {
        resp.send(Response.error(500, error));
        throw error;
      }
      resp.send(Response.ok());
    }
  );
});
//根据id查找电影院
//接口地址/cinema/sel
router.get("/cinema/sel", (req, resp) => {
  let { id } = req.query;
  //表单验证
  let schema = Joi.object({
    id: Joi.string().required(),
  });
  let { error, value } = schema.validate(req.query);
  if (error) {
    resp.send(Response.error(400, error));
    return;
  }
  let sql = "select * from movie_cinema where id= ?";

  pool.query(sql, [id], (error, result) => {
    if (error) {
      resp.send(Response.error(500, error));
      throw err;
    }
    if (result && result.length == 0) {
      resp.send(Response.ok(null));
    } else {
      resp.send(Response.ok(result[0]));
    }
  });
});
//删除电影院接口
//http://localhost:3000/cinema/del
router.post("/cinema/del", (req, resp) => {
  let { id } = req.body;
  //表单验证
  let schema = Joi.object({
    id: Joi.string().required(),
  });
  let { error, value } = schema.validate(req.body);
  if (error) {
    resp.send(Response.error(400, error));
    return;
  }
  //执行删除业务
  let sql = "delete from movie_cinema where id = ?";
  pool.query(sql, [id], (error, result) => {
    if (error) {
      resp.send(Response.error(500, error));
      throw error;
    }
    resp.send(Response.ok());
  });
});
//查询所有电影院
//http://localhost:3000/cinemas
router.get("/cinemas", (req, resp) => {
  let sql = "select * from movie_cinema";
  pool.query(sql, (error, result) => {
    if (error) {
      resp.send(Response.error(500, error));
      throw error;
    }
    resp.send(Response.ok(result));
  });
});
//查询所有标签
//http://localhost:3000/cinema/tags
router.get("/cinema/tags", (req, resp) => {
  let sql = "select * from movie_cinema_tag";
  pool.query(sql, (error, result) => {
    if (error) {
      resp.send(Response.error(500, error));
      throw error;
    }
    resp.send(Response.ok(result));
  });
});
//添加演员接口
//接口地址/cinema/add
router.post("/cinema/add", (req, resp) => {
  let {
    cinema_name,
    address,
    province,
    city,
    district,
    longitude,
    latitude,
    tags,
  } = req.body;
  //表单验证
  let schema = Joi.object({
    cinema_name: Joi.string().required(),
    address: Joi.string().required(),
    province: Joi.string().required(),
    city: Joi.string().required(),
    district: Joi.string().required(),
    longitude: Joi.number().required(),
    latitude: Joi.number().required(),
    tags: Joi.string().required(),
  });
  let { error, value } = schema.validate(req.body);
  if (error) {
    resp.send(Response.error(400, error));
    return;
  }
  //表单验证通过，执行添加操作
  let sql = `insert into movie_cinema ( cinema_name,
    address,
    province,
    city,
    district,
    longitude,
    latitude,
    tags) values (?,?,?,?,?,?,?,?)`;
  pool.query(
    sql,
    [cinema_name, address, province, city, district, longitude, latitude, tags],
    (error, result) => {
      if (error) {
        resp.send(Response.error(500, error));
        throw error;
      }
      resp.send(Response.ok());
    }
  );
});

//将Router接口导出
module.exports = router;
